package com.kantboot.user.account.service.impl;

import com.kantboot.functional.verification.code.domain.dto.SendVerificationCodeDTO;
import com.kantboot.functional.verification.code.domain.enums.VerificationCodeTypeCodeEnum;
import com.kantboot.functional.verification.code.service.IFunctionalVerificationCodeService;
import com.kantboot.user.account.domain.dto.UserAccountSendVerificationCodeDTO;
import com.kantboot.user.account.domain.entity.UserAccount;
import com.kantboot.user.account.domain.enums.UserAccountVerificationCodeSceneCodeEnums;
import com.kantboot.user.account.repostiroy.UserAccountRepository;
import com.kantboot.user.account.service.IUserAccountBindService;
import com.kantboot.user.account.service.IUserAccountService;
import com.kantboot.user.account.service.IUserAccountTokenService;
import com.kantboot.util.common.exception.BaseException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserAccountBindServiceImpl implements IUserAccountBindService {

    @Resource
    private IUserAccountService userAccountService;

    @Resource
    private UserAccountRepository repository;

    @Resource
    private IFunctionalVerificationCodeService verificationCodeService;

    @Resource
    private IUserAccountTokenService userAccountTokenService;

    @Override
    public void skipBind() {
        UserAccount userAccount = userAccountService.getSelf();
        userAccount.setIsTemporary(false);
        repository.save(userAccount);
    }

    @Override
    public void sendBindVerificationCode(UserAccountSendVerificationCodeDTO userAccountSendVerificationCodeDTO) {
        // 接受者
        String to = userAccountSendVerificationCodeDTO.getTo();
        // 手机区号，如果是邮箱则不需要
        String phoneAreaCode = userAccountSendVerificationCodeDTO.getPhoneAreaCode();
        // 类型编码
        String typeCode = userAccountSendVerificationCodeDTO.getTypeCode();

        SendVerificationCodeDTO sendVerificationCodeDTO = new SendVerificationCodeDTO()
                .setPhoneAreaCode(phoneAreaCode)
                .setTo(to)
                .setTypeCode(typeCode)
                .setSceneCode(UserAccountVerificationCodeSceneCodeEnums.LOGIN);
        verificationCodeService.send(sendVerificationCodeDTO);
    }

    @Override
    public UserAccount bindByPhoneAndVerificationCode(String phoneAreaCode, String phone, String verificationCode) {
        SendVerificationCodeDTO sendVerificationCodeDTO = new SendVerificationCodeDTO()
                .setTo(phone)
                .setTypeCode(VerificationCodeTypeCodeEnum.SMS)
                .setSceneCode(UserAccountVerificationCodeSceneCodeEnums.LOGIN);
        // 验证码校验
        Boolean verify = verificationCodeService.verify(sendVerificationCodeDTO, verificationCode);
        if (!verify) {
            throw BaseException.of("verificationCodeOfSmsError", "短信验证码错误");
        }
        // 根据手机号查询用户
        UserAccount byPhone = repository.findByPhoneAreaCodeAndPhone(phoneAreaCode, phone);
        if (byPhone == null) {
            // 如果该手机号未注册，自动注册
            UserAccount userAccountToSave = new UserAccount();
            userAccountToSave.setPhoneAreaCode(phoneAreaCode);
            userAccountToSave.setPhone(phone);
            byPhone = userAccountService.createUserAccount(userAccountToSave);
        }
        // 保存用户账号
        byPhone.setIsTemporary(false);
        UserAccount save = repository.save(byPhone);
        // 将密码置空
        save.setPassword(null);
        userAccountTokenService.switchUser(save.getId());
        return save;
    }

    @Override
    public UserAccount bindByEmailAndVerificationCode(String email, String verificationCode) {
        SendVerificationCodeDTO sendVerificationCodeDTO = new SendVerificationCodeDTO()
                .setTo(email)
                .setTypeCode(VerificationCodeTypeCodeEnum.EMAIL)
                .setSceneCode(UserAccountVerificationCodeSceneCodeEnums.LOGIN);
        // 验证码校验
        Boolean verify = verificationCodeService.verify(sendVerificationCodeDTO, verificationCode);
        if (!verify) {
            throw BaseException.of("verificationCodeOfEmailError", "邮箱验证码错误");
        }
        // 根据邮箱查询用户
        UserAccount byEmail = repository.findByEmail(email);
        if (byEmail == null) {
            UserAccount userAccountToSave = new UserAccount();
            userAccountToSave.setEmail(email);
            // 如果该邮箱未注册，自动注册
            byEmail = userAccountService.createUserAccount(userAccountToSave);
        }
        // 保存用户账号
        byEmail.setIsTemporary(false);
        UserAccount save = repository.save(byEmail);
        // 将密码置空
        save.setPassword(null);
        userAccountTokenService.switchUser(save.getId());
        return save;
    }
}
